package service.temp.job;

import com.alibaba.excel.EasyExcel;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import scala.collection.Seq;
import service.Product;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.*;

import static io.ExcelUtil.getCellFromRowByIndex;

/**
 * Created by fanzhenyu02 on 2022/6/08.
 */
public class JoinExcel {


    public static void main(String[] args) throws Exception {
        long startExecuteTime = System.currentTimeMillis();


        System.out.println("current match: " + "{NIKE]".contains("nike"));


//        List<Map<String, String>> audit_final_rowList = readContentFromExcel("/Users/zhfan/Downloads/matched_result_20220609_audit_final.xlsx");
//        Map<String, Map<String, String>> contains_match_matched_rowList = readContainsMatchContentFromExcel("/Users/zhfan/Downloads/contains_match_matched_result_20220617.xlsx");
//
//        List<List<Object>> writeList = new ArrayList<>();
//        for (Map<String, String> row : audit_final_rowList) {
//            String curContainsMatchedKeyword = null == contains_match_matched_rowList.get(row.get("vendorItemid")) ? "" : contains_match_matched_rowList.get(row.get("vendorItemid")).get("containsMatchedKeyword");
//            List<Object> curRow = new ArrayList<>();
//            curRow.add(row.get("vendorItemid"));
//            curRow.add(row.get("productName"));
//            curRow.add(row.get("TargetBrand"));
//            curRow.add(row.get("sellerInputBrand"));
//            curRow.add(row.get("gatedBrandKeyword"));
//            curRow.add(row.get("trueFalse"));
//            curRow.add(row.get("falseReason"));
//            curRow.add(curContainsMatchedKeyword);
//            writeList.add(curRow);
//        }
//
//        EasyExcel.write("/Users/zhfan/Downloads/matched_result_20220609_audit_final_with_compare.xlsx").head(generateMatchHead()).sheet("matched_result").doWrite(writeList);
        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - startExecuteTime) + " ms.");
    }


    private static List<List<String>> generateMatchHead() {
        List<List<String>> list = new ArrayList<>();
        list.add(new ArrayList<>(Arrays.asList("vendorItemid")));
        list.add(new ArrayList<>(Arrays.asList("productName")));
        list.add(new ArrayList<>(Arrays.asList("TargetBrand")));
        list.add(new ArrayList<>(Arrays.asList("sellerInputBrand")));
        list.add(new ArrayList<>(Arrays.asList("gatedBrandKeyword")));
        list.add(new ArrayList<>(Arrays.asList("trueFalse")));
        list.add(new ArrayList<>(Arrays.asList("falseReason")));
        list.add(new ArrayList<>(Arrays.asList("containsMatchedKeyword")));
        return list;
    }


    public static Map<String, Map<String, String>> readContainsMatchContentFromExcel(String fileFullName) {
        Map<String, Map<String, String>> lineList = new HashMap<>();
        try {
            Workbook workbook = WorkbookFactory.create(new File(fileFullName));
            Sheet sheet = workbook.getSheetAt(0);
            for (Row row : sheet) {
                Map<String, String> curLine = new HashMap<>();
                curLine.put("vendorItemid", getCellFromRowByIndex(row, 0));
                curLine.put("containsMatchedKeyword", getCellFromRowByIndex(row, 3));
                lineList.put(curLine.get("vendorItemid"), curLine);
            }
        } catch (Exception e) {
            System.err.println("readContainsMatchContentFromExcel got error!" + e);
        }

        return lineList;
    }


    public static List<Map<String, String>> readContentFromExcel(String fileFullName) {
        List<Map<String, String>> lineList = new ArrayList<>();
        try {
            Workbook workbook = WorkbookFactory.create(new File(fileFullName));
            Sheet sheet = workbook.getSheetAt(0);
            for (Row row : sheet) {
                Map<String, String> curLine = new HashMap<>();
                curLine.put("vendorItemid", getCellFromRowByIndex(row, 0));
                curLine.put("productName", getCellFromRowByIndex(row, 1));
                curLine.put("TargetBrand", getCellFromRowByIndex(row, 2));
                curLine.put("sellerInputBrand", getCellFromRowByIndex(row, 3));
                curLine.put("gatedBrandKeyword", getCellFromRowByIndex(row, 4));
                curLine.put("trueFalse", getCellFromRowByIndex(row, 5));
                curLine.put("falseReason", getCellFromRowByIndex(row, 6));

                lineList.add(curLine);
            }
        } catch (Exception e) {
            System.err.println("readContentFromExcel got error!" + e);
        }

        return lineList;
    }

}
